<?php
require_once('includes/config.php');
include('includes/sc-includes.php');

//
// Delete contact
//
if (isset($_GET['contact']))
{
	$contact_id = $_GET['contact'];

	//
	// No joins so we have to cascade ourselves
	//
	$histories = $sdb->select('history', "SELECT * FROM history WHERE history_contact = '$contact_id'");

	foreach ($histories as $history)
	{
		$sdb->deleteAttributes('history', $history['Name']);
	}

	$notes = $sdb->select('notes', "SELECT * FROM notes WHERE contact_id = '$contact_id'");
	foreach ($notes as $note)
	{
		$sdb->deleteAttributes('notes', $note['Name']);
	}

	//
	// Now we can delete the contact
	//
	$sdb->deleteAttributes('contact', $contact_id);

	set_msg('Contact Deleted');
	header('Location: contacts.php'); die;
}

//
// Delete note
//
if (isset($_GET['note']))
{
	$note_id = $_GET['note'];

	$sdb->deleteAttributes('notes', $note_id);

	set_msg('Note Deleted');

	$redirect = "contact-details.php?id=$contact_id";
	header(sprintf('Location: %s', $redirect)); die;
}
?>